home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / MESH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-02  |  37.8 KB  |  2,150 lines  |  [TEXT/CWIE]

  1. /****************************************************************************
  2. *                mesh.c
  3. *
  4. *  This module implements primitives for mesh objects.
  5. *
  6. *  This module was written by Dieter Bayer [DB].
  7. *
  8. *  from Persistence of Vision(tm) Ray Tracer
  9. *  Copyright 1996 Persistence of Vision Team
  10. *---------------------------------------------------------------------------
  11. *  NOTICE: This source code file is provided so that users may experiment
  12. *  with enhancements to POV-Ray and to port the software to platforms other
  13. *  than those supported by the POV-Ray Team.  There are strict rules under
  14. *  which you are permitted to use this file.  The rules are in the file
  15. *  named POVLEGAL.DOC which should be distributed with this file. If
  16. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  17. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  18. *  Forum.  The latest version of POV-Ray may be found there as well.
  19. *
  20. * This program is based on the popular DKB raytracer version 2.12.
  21. * DKBTrace was originally written by David K. Buck.
  22. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  23. *
  24. *****************************************************************************/
  25.  
  26. /****************************************************************************
  27. *
  28. *  Explanation:
  29. *
  30. *    -
  31. *
  32. *  Syntax:
  33. *
  34. *    mesh
  35. *    {
  36. *      triangle { <CORNER1>, <CORNER2>, <CORNER3>, texture { NAME } }
  37. *      smooth_triangle { <CORNER1>, <NORMAL1>, <CORNER2>, <NORMAL2>, <CORNER3>, <NORMAL3>, texture { NAME } }
  38. *      ...
  39. *      [ hierarchy FLAG ]
  40. *    }
  41. *
  42. *  ---
  43. *
  44. *  Feb 1995 : Creation. [DB]
  45. *
  46. *****************************************************************************/
  47.  
  48. #include "frame.h"
  49. #include "vector.h"
  50. #include "povproto.h"
  51. #include "bbox.h"
  52. #include "matrices.h"
  53. #include "objects.h"
  54. #include "mesh.h"
  55. #include "texture.h"
  56. #include "povray.h"
  57.  
  58.  
  59.  
  60. /*****************************************************************************
  61. * Local preprocessor defines
  62. ******************************************************************************/
  63.  
  64. #define DEPTH_TOLERANCE 1e-6
  65.  
  66. #define max3_coordinate(x,y,z) ((x > y) ? ((x > z) ? X : Z) : ((y > z) ? Y : Z))
  67.  
  68. #define HASH_SIZE 1000
  69.  
  70. #define INITIAL_NUMBER_OF_ENTRIES 256
  71.  
  72.  
  73. /*****************************************************************************
  74. * Local typedefs
  75. ******************************************************************************/
  76.  
  77. typedef struct Hash_Table_Struct HASH_TABLE;
  78.  
  79. struct Hash_Table_Struct
  80. {
  81.   int Index;
  82.   SNGL_VECT P;
  83.   HASH_TABLE *Next;
  84. };
  85.  
  86.  
  87.  
  88. /*****************************************************************************
  89. * Static functions
  90. ******************************************************************************/
  91.  
  92. static int Intersect_Mesh  PARAMS((RAY *Ray, MESH *Mesh, ISTACK *Depth_Stack));
  93. static int All_Mesh_Intersections  PARAMS((OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack));
  94. static int Inside_Mesh  PARAMS((VECTOR IPoint, OBJECT *Object));
  95. static void Mesh_Normal  PARAMS((VECTOR Result, OBJECT *Object, INTERSECTION *Inter));
  96. static void *Copy_Mesh  PARAMS((OBJECT *Object));
  97. static void Translate_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  98. static void Rotate_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  99. static void Scale_Mesh  PARAMS((OBJECT *Object, VECTOR Vector, TRANSFORM *Trans));
  100. static void Transform_Mesh  PARAMS((OBJECT *Object, TRANSFORM *Trans));
  101. static void Invert_Mesh  PARAMS((OBJECT *Object));
  102. static void Destroy_Mesh  PARAMS((OBJECT *Object));
  103.  
  104. static void compute_smooth_triangle PARAMS((MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3));
  105. static int intersect_mesh_triangle PARAMS((RAY *Ray, MESH *Mesh, MESH_TRIANGLE *Triangle, DBL *Depth));
  106. static int test_hit PARAMS((MESH_TRIANGLE *Triangle, MESH *Mesh, RAY *Ray, DBL Depth, ISTACK *Depth_Stack));
  107. static void smooth_mesh_normal PARAMS((MESH *Mesh, VECTOR Result, MESH_TRIANGLE *Triangle, VECTOR IPoint));
  108. static void get_triangle_bbox PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, BBOX *BBox));
  109.  
  110. static int intersect_bbox_tree PARAMS((MESH *Mesh, RAY *Ray, RAY *Orig_Ray, DBL len, ISTACK *Depth_Stack));
  111.  
  112. static void get_triangle_vertices PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR P1, VECTOR P2, VECTOR P3));
  113. static void get_triangle_normals PARAMS((MESH *Mesh, MESH_TRIANGLE *Triangle, VECTOR N1, VECTOR N2, VECTOR N3));
  114.  
  115. static int mesh_hash PARAMS((HASH_TABLE **Hash_Table,
  116.   int *Number, int *Max, SNGL_VECT **Elements, VECTOR aPoint));
  117.  
  118.  
  119.  
  120. /*****************************************************************************
  121. * Local variables
  122. ******************************************************************************/
  123.  
  124. METHODS Mesh_Methods =
  125. {
  126.   All_Mesh_Intersections,
  127.   Inside_Mesh, Mesh_Normal,
  128.   Copy_Mesh,
  129.   Translate_Mesh, Rotate_Mesh,
  130.   Scale_Mesh, Transform_Mesh, Invert_Mesh, Destroy_Mesh
  131. };
  132.  
  133. static HASH_TABLE **Vertex_Hash_Table, **Normal_Hash_Table;
  134.  
  135. static PRIORITY_QUEUE *Mesh_Queue;
  136.  
  137.  
  138.  
  139. /*****************************************************************************
  140. *
  141. * FUNCTION
  142. *
  143. *   All_Mesh_Intersections
  144. *
  145. * INPUT
  146. *   
  147. * OUTPUT
  148. *   
  149. * RETURNS
  150. *   
  151. * AUTHOR
  152. *
  153. *   Dieter Bayer
  154. *   
  155. * DESCRIPTION
  156. *
  157. *   -
  158. *
  159. * CHANGES
  160. *
  161. *   Feb 1995 : Creation.
  162. *
  163. ******************************************************************************/
  164.  
  165. static int All_Mesh_Intersections(Object, Ray, Depth_Stack)
  166. OBJECT *Object;
  167. RAY *Ray;
  168. ISTACK *Depth_Stack;
  169. {
  170.   Increase_Counter(stats[Ray_Mesh_Tests]);
  171.  
  172.   if (Intersect_Mesh(Ray, (MESH *)Object, Depth_Stack))
  173.   {
  174.     Increase_Counter(stats[Ray_Mesh_Tests_Succeeded]);
  175.  
  176.     return(TRUE);
  177.   }
  178.  
  179.   return(FALSE);
  180. }
  181.  
  182.  
  183.  
  184. /*****************************************************************************
  185. *
  186. * FUNCTION
  187. *
  188. *   Intersect_Mesh
  189. *
  190. * INPUT
  191. *   
  192. * OUTPUT
  193. *   
  194. * RETURNS
  195. *   
  196. * AUTHOR
  197. *
  198. *   Dieter Bayer
  199. *   
  200. * DESCRIPTION
  201. *
  202. *   -
  203. *
  204. * CHANGES
  205. *
  206. *   Feb 1995 : Creation.
  207. *
  208. ******************************************************************************/
  209.  
  210. static int Intersect_Mesh(Ray, Mesh, Depth_Stack)
  211. RAY *Ray;
  212. MESH *Mesh;
  213. ISTACK *Depth_Stack;
  214. {
  215.   int i;
  216.   unsigned found;
  217.   DBL len, t;
  218.   RAY New_Ray;
  219.  
  220.   /* Transform the ray into mesh space. */
  221.  
  222.   if (Mesh->Trans != NULL)
  223.   {
  224.     MInvTransPoint(New_Ray.Initial, Ray->Initial, Mesh->Trans);
  225.     MInvTransDirection(New_Ray.Direction, Ray->Direction, Mesh->Trans);
  226.  
  227.     VLength(len, New_Ray.Direction);
  228.     VInverseScaleEq(New_Ray.Direction, len);
  229.   }
  230.   else
  231.   {
  232.     New_Ray = *Ray;
  233.  
  234.     len = 1.0;
  235.   }
  236.  
  237.   found = FALSE;
  238.  
  239.   if (Mesh->Data->Tree == NULL)
  240.   {
  241.     /* There's no bounding hierarchy so just step through all elements. */
  242.  
  243.     for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  244.     {
  245.       if (intersect_mesh_triangle(&New_Ray, Mesh, &Mesh->Data->Triangles[i], &t))
  246.       {
  247.         if (test_hit(&Mesh->Data->Triangles[i], Mesh, Ray, t / len, Depth_Stack))
  248.         {
  249.           found = TRUE;
  250.         }
  251.       }
  252.     }
  253.   }
  254.   else
  255.   {
  256.     /* Use the mesh's bounding hierarchy. */
  257.  
  258.     return(intersect_bbox_tree(Mesh, &New_Ray, Ray, len, Depth_Stack));
  259.   }
  260.  
  261.   return(found);
  262. }
  263.  
  264.  
  265.  
  266. /*****************************************************************************
  267. *
  268. * FUNCTION
  269. *
  270. *   Inside_Mesh
  271. *
  272. * INPUT
  273. *   
  274. * OUTPUT
  275. *   
  276. * RETURNS
  277. *   
  278. * AUTHOR
  279. *
  280. *   Dieter Bayer
  281. *   
  282. * DESCRIPTION
  283. *
  284. *   -
  285. *
  286. * CHANGES
  287. *
  288. *   Feb 1995 : Creation.
  289. *
  290. ******************************************************************************/
  291.  
  292. static int Inside_Mesh(IPoint, Object)
  293. VECTOR IPoint;
  294. OBJECT *Object;
  295. {
  296.   return(FALSE);
  297. }
  298.  
  299.  
  300.  
  301. /*****************************************************************************
  302. *
  303. * FUNCTION
  304. *
  305. *   Mesh_Normal
  306. *
  307. * INPUT
  308. *   
  309. * OUTPUT
  310. *   
  311. * RETURNS
  312. *   
  313. * AUTHOR
  314. *
  315. *   Dieter Bayer
  316. *   
  317. * DESCRIPTION
  318. *
  319. *   Return the normalized normal in the given point.
  320. *
  321. * CHANGES
  322. *
  323. *   Feb 1995 : Creation.
  324. *
  325. ******************************************************************************/
  326.  
  327. static void Mesh_Normal(Result, Object, Inter)
  328. OBJECT *Object;
  329. VECTOR Result;
  330. INTERSECTION *Inter;
  331. {
  332.   VECTOR IPoint;
  333.   MESH_TRIANGLE *Triangle;
  334.   MESH *Mesh = (MESH *)Object;
  335.  
  336.   Triangle = (MESH_TRIANGLE *)Inter->Pointer;
  337.  
  338.   if (Triangle->Smooth)
  339.   {
  340.     if (Mesh->Trans != NULL)
  341.     {
  342.       MInvTransPoint(IPoint, Inter->IPoint, Mesh->Trans);
  343.     }
  344.     else
  345.     {
  346.       Assign_Vector(IPoint, Inter->IPoint);
  347.     }
  348.  
  349.     smooth_mesh_normal(Mesh, Result, Triangle, IPoint);
  350.   
  351.     if (Mesh->Trans != NULL)
  352.     {
  353.       MTransNormal(Result, Result, Mesh->Trans);
  354.     }
  355.  
  356.     VNormalize(Result, Result);
  357.   }
  358.   else
  359.   {
  360.     Assign_SNGL_Vect(Result, Mesh->Data->Normals[Triangle->Normal_Ind]);
  361.  
  362.     if (Mesh->Trans != NULL)
  363.     {
  364.       MTransNormal(Result, Result, Mesh->Trans);
  365.  
  366.       VNormalize(Result, Result);
  367.     }
  368.   }
  369. }
  370.  
  371.  
  372.  
  373. /*****************************************************************************
  374. *
  375. * FUNCTION
  376. *
  377. *   smooth_mesh_normal
  378. *
  379. * INPUT
  380. *   
  381. * OUTPUT
  382. *   
  383. * RETURNS
  384. *   
  385. * AUTHOR
  386. *
  387. *   Dieter Bayer
  388. *   
  389. * DESCRIPTION
  390. *
  391. *   Remove the un-normalized normal of a smoothed triangle.
  392. *
  393. * CHANGES
  394. *
  395. *   Feb 1995 : Creation. (Derived from TRIANGLE.C)
  396. *
  397. ******************************************************************************/
  398.  
  399. static void smooth_mesh_normal(Mesh, Result, Triangle, IPoint)
  400. MESH *Mesh;
  401. VECTOR Result, IPoint;
  402. MESH_TRIANGLE *Triangle;
  403. {
  404.   int axis;
  405.   DBL u, v;
  406.   DBL k1, k2, k3;
  407.   VECTOR PIMinusP1, N1, N2, N3;
  408.  
  409.   get_triangle_normals(Mesh, Triangle, N1, N2, N3);
  410.  
  411.   VSub(PIMinusP1, IPoint, Mesh->Data->Vertices[Triangle->P1]);
  412.  
  413.   VDot(u, PIMinusP1, Triangle->Perp);
  414.  
  415.   if (u < EPSILON)
  416.   {
  417.     Assign_Vector(Result, N1);
  418.   }
  419.   else
  420.   {
  421.     axis = Triangle->vAxis;
  422.  
  423.     k1 = Mesh->Data->Vertices[Triangle->P1][axis];
  424.     k2 = Mesh->Data->Vertices[Triangle->P2][axis];
  425.     k3 = Mesh->Data->Vertices[Triangle->P3][axis];
  426.  
  427.     v = (PIMinusP1[axis] / u + k1 - k2) / (k3 - k2);
  428.  
  429.     Result[X] = N1[X] + u * (N2[X] - N1[X] + v * (N3[X] - N2[X]));
  430.     Result[Y] = N1[Y] + u * (N2[Y] - N1[Y] + v * (N3[Y] - N2[Y]));
  431.     Result[Z] = N1[Z] + u * (N2[Z] - N1[Z] + v * (N3[Z] - N2[Z]));
  432.   }
  433. }
  434.  
  435.  
  436.  
  437. /*****************************************************************************
  438. *
  439. * FUNCTION
  440. *
  441. *   Translate_Mesh
  442. *
  443. * INPUT
  444. *   
  445. * OUTPUT
  446. *   
  447. * RETURNS
  448. *   
  449. * AUTHOR
  450. *
  451. *   Dieter Bayer
  452. *   
  453. * DESCRIPTION
  454. *
  455. *   -
  456. *
  457. * CHANGES
  458. *
  459. *   Feb 1995 : Creation.
  460. *
  461. ******************************************************************************/
  462.  
  463. static void Translate_Mesh(Object, Vector, Trans)
  464. OBJECT *Object;
  465. VECTOR Vector;
  466. TRANSFORM *Trans;
  467. {
  468.   Transform_Mesh(Object, Trans);
  469. }
  470.  
  471.  
  472.  
  473. /*****************************************************************************
  474. *
  475. * FUNCTION
  476. *
  477. *   Rotate_Mesh
  478. *
  479. * INPUT
  480. *   
  481. * OUTPUT
  482. *   
  483. * RETURNS
  484. *   
  485. * AUTHOR
  486. *
  487. *   Dieter Bayer
  488. *   
  489. * DESCRIPTION
  490. *
  491. *   -
  492. *
  493. * CHANGES
  494. *
  495. *   Feb 1995 : Creation.
  496. *
  497. ******************************************************************************/
  498.  
  499. static void Rotate_Mesh(Object, Vector, Trans)
  500. OBJECT *Object;
  501. VECTOR Vector;
  502. TRANSFORM *Trans;
  503. {
  504.   Transform_Mesh(Object, Trans);
  505. }
  506.  
  507.  
  508.  
  509. /*****************************************************************************
  510. *
  511. * FUNCTION
  512. *
  513. *   Scale_Mesh
  514. *
  515. * INPUT
  516. *   
  517. * OUTPUT
  518. *   
  519. * RETURNS
  520. *   
  521. * AUTHOR
  522. *
  523. *   Dieter Bayer
  524. *   
  525. * DESCRIPTION
  526. *
  527. *   -
  528. *
  529. * CHANGES
  530. *
  531. *   Feb 1995 : Creation.
  532. *
  533. ******************************************************************************/
  534.  
  535. static void Scale_Mesh(Object, Vector, Trans)
  536. OBJECT *Object;
  537. VECTOR Vector;
  538. TRANSFORM *Trans;
  539. {
  540.   Transform_Mesh(Object, Trans);
  541. }
  542.  
  543.  
  544.  
  545. /*****************************************************************************
  546. *
  547. * FUNCTION
  548. *
  549. *   Transfrom_Mesh
  550. *
  551. * INPUT
  552. *   
  553. * OUTPUT
  554. *   
  555. * RETURNS
  556. *   
  557. * AUTHOR
  558. *
  559. *   Dieter Bayer
  560. *   
  561. * DESCRIPTION
  562. *
  563. *   -
  564. *
  565. * CHANGES
  566. *
  567. *   Feb 1995 : Creation.
  568. *
  569. ******************************************************************************/
  570.  
  571. static void Transform_Mesh(Object, Trans)
  572. OBJECT *Object;
  573. TRANSFORM *Trans;
  574. {
  575.   if (((MESH *)Object)->Trans == NULL)
  576.   {
  577.     ((MESH *)Object)->Trans = Create_Transform();
  578.   }
  579.  
  580.   Recompute_BBox(&Object->BBox, Trans);
  581.  
  582.   Compose_Transforms(((MESH *)Object)->Trans, Trans);
  583. }
  584.  
  585.  
  586.  
  587. /*****************************************************************************
  588. *
  589. * FUNCTION
  590. *
  591. *   Invert_Mesh
  592. *
  593. * INPUT
  594. *   
  595. * OUTPUT
  596. *   
  597. * RETURNS
  598. *   
  599. * AUTHOR
  600. *
  601. *   Dieter Bayer
  602. *   
  603. * DESCRIPTION
  604. *
  605. *   -
  606. *
  607. * CHANGES
  608. *
  609. *   Feb 1995 : Creation.
  610. *
  611. ******************************************************************************/
  612.  
  613. static void Invert_Mesh(Object)
  614. OBJECT *Object;
  615. {
  616. }
  617.  
  618.  
  619.  
  620. /*****************************************************************************
  621. *
  622. * FUNCTION
  623. *
  624. *   Create_Mesh
  625. *
  626. * INPUT
  627. *   
  628. * OUTPUT
  629. *   
  630. * RETURNS
  631. *   
  632. * AUTHOR
  633. *
  634. *   Dieter Bayer
  635. *   
  636. * DESCRIPTION
  637. *
  638. *   -
  639. *
  640. * CHANGES
  641. *
  642. *   Feb 1995 : Creation.
  643. *
  644. ******************************************************************************/
  645.  
  646. MESH *Create_Mesh()
  647. {
  648.   MESH *New;
  649.  
  650.   New = (MESH *)POV_MALLOC(sizeof(MESH), "mesh");
  651.  
  652.   INIT_OBJECT_FIELDS(New,MESH_OBJECT,&Mesh_Methods)
  653.  
  654.   Set_Flag(New, HIERARCHY_FLAG);
  655.  
  656.   New->Trans = NULL;
  657.  
  658.   New->Data = NULL;
  659.  
  660.   return(New);
  661. }
  662.  
  663.  
  664.  
  665. /*****************************************************************************
  666. *
  667. * FUNCTION
  668. *
  669. *   Copy_Mesh
  670. *
  671. * INPUT
  672. *   
  673. * OUTPUT
  674. *   
  675. * RETURNS
  676. *   
  677. * AUTHOR
  678. *
  679. *   Dieter Bayer
  680. *   
  681. * DESCRIPTION
  682. *
  683. *   Copy a mesh.
  684. *
  685. *   NOTE: The components are not copied, only the number of references is
  686. *         counted, so that Destroy_Mesh() knows if they can be destroyed.
  687. *
  688. *   -
  689. *
  690. * CHANGES
  691. *
  692. *   Feb 1995 : Creation.
  693. *
  694. ******************************************************************************/
  695.  
  696. static void *Copy_Mesh(Object)
  697. OBJECT *Object;
  698. {
  699.   MESH *New;
  700.  
  701.   New = Create_Mesh();
  702.  
  703.   /* Copy mesh. */
  704.  
  705.   *New = *((MESH *)Object);
  706.   
  707.   New->Trans = Copy_Transform(New->Trans);
  708.   
  709.   New->Data->References++;
  710.  
  711.   return(New);
  712. }
  713.  
  714.  
  715.  
  716. /*****************************************************************************
  717. *
  718. * FUNCTION
  719. *
  720. *   Destroy_Mesh
  721. *
  722. * INPUT
  723. *   
  724. * OUTPUT
  725. *   
  726. * RETURNS
  727. *   
  728. * AUTHOR
  729. *
  730. *   Dieter Bayer
  731. *   
  732. * DESCRIPTION
  733. *
  734. *   -
  735. *
  736. * CHANGES
  737. *
  738. *   Feb 1995 : Creation.
  739. *
  740. ******************************************************************************/
  741.  
  742. static void Destroy_Mesh(Object)
  743. OBJECT *Object;
  744. {
  745.   int i;
  746.   MESH *Mesh = (MESH *)Object;
  747.  
  748.   Destroy_Transform(Mesh->Trans);
  749.  
  750.   if (--(Mesh->Data->References) == 0)
  751.   {
  752.     Destroy_BBox_Tree(Mesh->Data->Tree);
  753.  
  754.     if (Mesh->Data->Normals != NULL)
  755.     {
  756.       POV_FREE(Mesh->Data->Normals);
  757.     }
  758.  
  759.     if (Mesh->Data->Vertices != NULL)
  760.     {
  761.       POV_FREE(Mesh->Data->Vertices);
  762.     }
  763.  
  764.     if (Mesh->Data->Triangles != NULL)
  765.     {
  766.       POV_FREE(Mesh->Data->Triangles);
  767.     }
  768.  
  769.     if (Mesh->Data->Textures != NULL)
  770.     {
  771.       for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  772.       {
  773.         Destroy_Textures(Mesh->Data->Textures[i]);
  774.       }
  775.  
  776.       POV_FREE(Mesh->Data->Textures);
  777.     }
  778.  
  779.     POV_FREE(Mesh->Data);
  780.   }
  781.  
  782.   POV_FREE(Object);
  783. }
  784.  
  785.  
  786.  
  787. /*****************************************************************************
  788. *
  789. * FUNCTION
  790. *
  791. *   Compute_Mesh_BBox
  792. *
  793. * INPUT
  794. *
  795. *   Mesh - Mesh
  796. *   
  797. * OUTPUT
  798. *
  799. *   Mesh
  800. *   
  801. * RETURNS
  802. *   
  803. * AUTHOR
  804. *
  805. *   Dieter Bayer
  806. *   
  807. * DESCRIPTION
  808. *
  809. *   Calculate the bounding box of a triangle.
  810. *
  811. * CHANGES
  812. *
  813. *   Feb 1995 : Creation.
  814. *
  815. ******************************************************************************/
  816.  
  817. void Compute_Mesh_BBox(Mesh)
  818. MESH *Mesh;
  819. {
  820.   int i;
  821.   VECTOR P1, P2, P3;
  822.   VECTOR mins, maxs;
  823.  
  824.   Make_Vector(mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  825.   Make_Vector(maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  826.  
  827.   for (i = 0; i < Mesh->Data->Number_Of_Triangles; i++)
  828.   {
  829.     get_triangle_vertices(Mesh, &Mesh->Data->Triangles[i], P1, P2, P3);
  830.  
  831.     mins[X] = min(mins[X], min3(P1[X], P2[X], P3[X]));
  832.     mins[Y] = min(mins[Y], min3(P1[Y], P2[Y], P3[Y]));
  833.     mins[Z] = min(mins[Z], min3(P1[Z], P2[Z], P3[Z]));
  834.  
  835.     maxs[X] = max(maxs[X], max3(P1[X], P2[X], P3[X]));
  836.     maxs[Y] = max(maxs[Y], max3(P1[Y], P2[Y], P3[Y]));
  837.     maxs[Z] = max(maxs[Z], max3(P1[Z], P2[Z], P3[Z]));
  838.   }
  839.  
  840.   Make_BBox_from_min_max(Mesh->BBox, mins, maxs);
  841. }
  842.  
  843.  
  844.  
  845. /*****************************************************************************
  846. *
  847. * FUNCTION
  848. *
  849. *   Compute_Mesh
  850. *
  851. * INPUT
  852. *   
  853. * OUTPUT
  854. *   
  855. * RETURNS
  856. *   
  857. * AUTHOR
  858. *
  859. *   Dieter Bayer
  860. *   
  861. * DESCRIPTION
  862. *
  863. *   -
  864. *
  865. * CHANGES
  866. *
  867. *   Feb 1995 : Creation.
  868. *
  869. ******************************************************************************/
  870.  
  871. int Compute_Mesh_Triangle(Triangle, Smooth, P1, P2, P3, S_Normal)
  872. MESH_TRIANGLE *Triangle;
  873. int Smooth;
  874. VECTOR P1, P2, P3, S_Normal;
  875. {
  876.   int temp, swap;
  877.   DBL x, y, z;
  878.   VECTOR V1, V2, T1;
  879.   DBL Length;
  880.  
  881.   VSub(V1, P2, P1);
  882.   VSub(V2, P3, P1);
  883.  
  884.   VCross(S_Normal, V2, V1);
  885.  
  886.   VLength(Length, S_Normal);
  887.  
  888.   /* Set up a flag so we can ignore degenerate triangles */
  889.  
  890.   if (Length == 0.0)
  891.   {
  892.     return(FALSE);
  893.   }
  894.  
  895.   /* Normalize the normal vector. */
  896.  
  897.   VInverseScaleEq(S_Normal, Length);
  898.  
  899.   VDot(Triangle->Distance, S_Normal, P1);
  900.  
  901.   Triangle->Distance *= -1.0;
  902.  
  903.   /* Find triangle's dominant axis. */
  904.  
  905.   x = fabs(S_Normal[X]);
  906.   y = fabs(S_Normal[Y]);
  907.   z = fabs(S_Normal[Z]);
  908.  
  909.   Triangle->Dominant_Axis = max3_coordinate(x, y, z);
  910.  
  911.   swap = FALSE;
  912.  
  913.   switch (Triangle->Dominant_Axis)
  914.   {
  915.     case X:
  916.  
  917.       if ((P2[Y] - P3[Y])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[Y] - P1[Y]))
  918.       {
  919.         swap = TRUE;
  920.       }
  921.  
  922.       break;
  923.  
  924.     case Y:
  925.  
  926.       if ((P2[X] - P3[X])*(P2[Z] - P1[Z]) < (P2[Z] - P3[Z])*(P2[X] - P1[X]))
  927.       {
  928.         swap = TRUE;
  929.       }
  930.  
  931.       break;
  932.  
  933.     case Z:
  934.  
  935.       if ((P2[X] - P3[X])*(P2[Y] - P1[Y]) < (P2[Y] - P3[Y])*(P2[X] - P1[X]))
  936.       {
  937.         swap = TRUE;
  938.       }
  939.  
  940.       break;
  941.   }
  942.  
  943.   if (swap)
  944.   {
  945.     temp = Triangle->P2;
  946.     Triangle->P2 = Triangle->P1;
  947.     Triangle->P1 = temp;
  948.  
  949.     Assign_Vector(T1, P1);
  950.     Assign_Vector(P1, P2);
  951.     Assign_Vector(P2, T1);
  952.  
  953.     if (Smooth)
  954.     {
  955.       temp = Triangle->N2;
  956.       Triangle->N2 = Triangle->N1;
  957.       Triangle->N1 = temp;
  958.     }
  959.   }
  960.  
  961.   if (Smooth)
  962.   {
  963.     compute_smooth_triangle(Triangle, P1, P2, P3);
  964.   }
  965.  
  966.   return(TRUE);
  967. }
  968.  
  969.  
  970.  
  971. /*****************************************************************************
  972. *
  973. * FUNCTION
  974. *
  975. *   compute_smooth_triangle
  976. *
  977. * INPUT
  978. *   
  979. * OUTPUT
  980. *   
  981. * RETURNS
  982. *   
  983. * AUTHOR
  984. *
  985. *   Dieter Bayer
  986. *   
  987. * DESCRIPTION
  988. *
  989. *   -
  990. *
  991. * CHANGES
  992. *
  993. *   Feb 1995 : Creation.
  994. *
  995. ******************************************************************************/
  996.  
  997. static void compute_smooth_triangle(Triangle, P1, P2, P3)
  998. MESH_TRIANGLE *Triangle;
  999. VECTOR P1, P2, P3;
  1000. {
  1001.   VECTOR P3MinusP2, VTemp1, VTemp2;
  1002.   DBL x, y, z, uDenominator, Proj;
  1003.  
  1004.   Triangle->Smooth = TRUE;
  1005.  
  1006.   VSub(P3MinusP2, P3, P2);
  1007.  
  1008.   x = fabs(P3MinusP2[X]);
  1009.   y = fabs(P3MinusP2[Y]);
  1010.   z = fabs(P3MinusP2[Z]);
  1011.  
  1012.   Triangle->vAxis = max3_coordinate(x, y, z);
  1013.  
  1014.   VSub(VTemp1, P2, P3);
  1015.  
  1016.   VNormalize(VTemp1, VTemp1);
  1017.  
  1018.   VSub(VTemp2, P1, P3);
  1019.  
  1020.   VDot(Proj, VTemp2, VTemp1);
  1021.  
  1022.   VScaleEq(VTemp1, Proj);
  1023.  
  1024.   VSub(Triangle->Perp, VTemp1, VTemp2);
  1025.  
  1026.   VNormalize(Triangle->Perp, Triangle->Perp);
  1027.  
  1028.   VDot(uDenominator, VTemp2, Triangle->Perp);
  1029.  
  1030.   VInverseScaleEq(Triangle->Perp, -uDenominator);
  1031. }
  1032.  
  1033.  
  1034.  
  1035. /*****************************************************************************
  1036. *
  1037. * FUNCTION
  1038. *
  1039. *   intersect_mesh_triangle
  1040. *
  1041. * INPUT
  1042. *   
  1043. * OUTPUT
  1044. *   
  1045. * RETURNS
  1046. *   
  1047. * AUTHOR
  1048. *
  1049. *   Dieter Bayer
  1050. *   
  1051. * DESCRIPTION
  1052. *
  1053. *   -
  1054. *
  1055. * CHANGES
  1056. *
  1057. *   Feb 1995 : Creation.
  1058. *
  1059. ******************************************************************************/
  1060.  
  1061. static int intersect_mesh_triangle(Ray, Mesh, Triangle, Depth)
  1062. RAY *Ray;
  1063. MESH *Mesh;
  1064. MESH_TRIANGLE *Triangle;
  1065. DBL *Depth;
  1066. {
  1067.   DBL NormalDotOrigin, NormalDotDirection;
  1068.   DBL s, t;
  1069.   VECTOR P1, P2, P3, S_Normal;
  1070.  
  1071.   Assign_SNGL_Vect(S_Normal, Mesh->Data->Normals[Triangle->Normal_Ind]);
  1072.  
  1073.   VDot(NormalDotDirection, S_Normal, Ray->Direction);
  1074.  
  1075.   if (fabs(NormalDotDirection) < EPSILON)
  1076.   {
  1077.     return(FALSE);
  1078.   }
  1079.  
  1080.   VDot(NormalDotOrigin, S_Normal, Ray->Initial);
  1081.  
  1082.   *Depth = -(Triangle->Distance + NormalDotOrigin) / NormalDotDirection;
  1083.  
  1084.   if ((*Depth < DEPTH_TOLERANCE) || (*Depth > Max_Distance))
  1085.   {
  1086.     return(FALSE);
  1087.   }
  1088.  
  1089.   get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  1090.  
  1091.   switch (Triangle->Dominant_Axis)
  1092.   {
  1093.     case X:
  1094.  
  1095.       s = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  1096.       t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  1097.  
  1098.       if ((P2[Y] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[Y] - P1[Y]))
  1099.       {
  1100.         return(FALSE);
  1101.       }
  1102.  
  1103.       if ((P3[Y] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[Y] - P2[Y]))
  1104.       {
  1105.         return(FALSE);
  1106.       }
  1107.  
  1108.       if ((P1[Y] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[Y] - P3[Y]))
  1109.       {
  1110.         return(FALSE);
  1111.       }
  1112.  
  1113.       return(TRUE);
  1114.  
  1115.     case Y:
  1116.  
  1117.       s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  1118.       t = Ray->Initial[Z] + *Depth * Ray->Direction[Z];
  1119.  
  1120.       if ((P2[X] - s) * (P2[Z] - P1[Z]) < (P2[Z] - t) * (P2[X] - P1[X]))
  1121.       {
  1122.         return(FALSE);
  1123.       }
  1124.  
  1125.       if ((P3[X] - s) * (P3[Z] - P2[Z]) < (P3[Z] - t) * (P3[X] - P2[X]))
  1126.       {
  1127.         return(FALSE);
  1128.       }
  1129.  
  1130.       if ((P1[X] - s) * (P1[Z] - P3[Z]) < (P1[Z] - t) * (P1[X] - P3[X]))
  1131.       {
  1132.         return(FALSE);
  1133.       }
  1134.  
  1135.       return(TRUE);
  1136.  
  1137.     case Z:
  1138.  
  1139.       s = Ray->Initial[X] + *Depth * Ray->Direction[X];
  1140.       t = Ray->Initial[Y] + *Depth * Ray->Direction[Y];
  1141.  
  1142.       if ((P2[X] - s) * (P2[Y] - P1[Y]) < (P2[Y] - t) * (P2[X] - P1[X]))
  1143.       {
  1144.         return(FALSE);
  1145.       }
  1146.  
  1147.       if ((P3[X] - s) * (P3[Y] - P2[Y]) < (P3[Y] - t) * (P3[X] - P2[X]))
  1148.       {
  1149.         return(FALSE);
  1150.       }
  1151.  
  1152.       if ((P1[X] - s) * (P1[Y] - P3[Y]) < (P1[Y] - t) * (P1[X] - P3[X]))
  1153.       {
  1154.         return(FALSE);
  1155.       }
  1156.  
  1157.       return(TRUE);
  1158.   }
  1159.  
  1160.   return(FALSE);
  1161. }
  1162.  
  1163.  
  1164.  
  1165. /*****************************************************************************
  1166. *
  1167. * FUNCTION
  1168. *
  1169. *   test_hit
  1170. *
  1171. * INPUT
  1172. *   
  1173. * OUTPUT
  1174. *   
  1175. * RETURNS
  1176. *   
  1177. * AUTHOR
  1178. *
  1179. *   Dieter Bayer
  1180. *   
  1181. * DESCRIPTION
  1182. *
  1183. *   Test if a hit is valid and push if on the intersection depth.
  1184. *
  1185. * CHANGES
  1186. *
  1187. *   Feb 1995 : Creation.
  1188. *
  1189. ******************************************************************************/
  1190.  
  1191. static int test_hit(Triangle, Mesh, Ray, Depth, Depth_Stack)
  1192. MESH_TRIANGLE *Triangle;
  1193. MESH *Mesh;
  1194. RAY *Ray;
  1195. DBL Depth;
  1196. ISTACK *Depth_Stack;
  1197. {
  1198.   VECTOR IPoint;
  1199.   OBJECT *Object = (OBJECT *)Mesh;
  1200.  
  1201.   VEvaluateRay(IPoint, Ray->Initial, Depth, Ray->Direction);
  1202.  
  1203.   if (Point_In_Clip(IPoint, Object->Clip))
  1204.   {
  1205.     push_entry_pointer(Depth, IPoint, Object, Triangle, Depth_Stack);
  1206.  
  1207.     return(TRUE);
  1208.   }
  1209.  
  1210.   return(FALSE);
  1211. }
  1212.  
  1213.  
  1214.  
  1215. /*****************************************************************************
  1216. *
  1217. * FUNCTION
  1218. *
  1219. *   Init_Mesh_Triangle
  1220. *
  1221. * INPUT
  1222. *   
  1223. * OUTPUT
  1224. *   
  1225. * RETURNS
  1226. *   
  1227. * AUTHOR
  1228. *
  1229. *   Dieter Bayer
  1230. *   
  1231. * DESCRIPTION
  1232. *
  1233. *   -
  1234. *
  1235. * CHANGES
  1236. *
  1237. *   Feb 1995 : Creation.
  1238. *
  1239. ******************************************************************************/
  1240.  
  1241. void Init_Mesh_Triangle(Triangle)
  1242. MESH_TRIANGLE *Triangle;
  1243. {
  1244.   Triangle->Smooth = FALSE;
  1245.  
  1246.   Triangle->Dominant_Axis = 0;
  1247.   Triangle->vAxis         = 0;
  1248.  
  1249.   Triangle->P1 =
  1250.   Triangle->P2 =
  1251.   Triangle->P3 = -1;
  1252.  
  1253.   Triangle->Normal_Ind = -1;
  1254.  
  1255.   Triangle->Texture = -1;
  1256.  
  1257.   Triangle->N1 =
  1258.   Triangle->N2 =
  1259.   Triangle->N3 = -1;
  1260.  
  1261.   Make_Vector(Triangle->Perp, 0.0, 0.0, 0.0);
  1262.  
  1263.   Triangle->Distance = 0.0;
  1264. }
  1265.  
  1266.  
  1267.  
  1268. /*****************************************************************************
  1269. *
  1270. * FUNCTION
  1271. *
  1272. *   get_triangle_bbox
  1273. *
  1274. * INPUT
  1275. *
  1276. *   Triangle - Pointer to triangle
  1277. *   
  1278. * OUTPUT
  1279. *
  1280. *   BBox     - Bounding box
  1281. *   
  1282. * RETURNS
  1283. *   
  1284. * AUTHOR
  1285. *
  1286. *   Dieter Bayer
  1287. *   
  1288. * DESCRIPTION
  1289. *
  1290. *   Calculate the bounding box of a triangle.
  1291. *
  1292. * CHANGES
  1293. *
  1294. *   Sep 1994 : Creation.
  1295. *
  1296. ******************************************************************************/
  1297.  
  1298. static void get_triangle_bbox(Mesh, Triangle, BBox)
  1299. MESH *Mesh;
  1300. MESH_TRIANGLE *Triangle;
  1301. BBOX *BBox;
  1302. {
  1303.   VECTOR P1, P2, P3;
  1304.   VECTOR Min, Max;
  1305.  
  1306.   get_triangle_vertices(Mesh, Triangle, P1, P2, P3);
  1307.  
  1308.   Min[X] = min3(P1[X], P2[X], P3[X]);
  1309.   Min[Y] = min3(P1[Y], P2[Y], P3[Y]);
  1310.   Min[Z] = min3(P1[Z], P2[Z], P3[Z]);
  1311.  
  1312.   Max[X] = max3(P1[X], P2[X], P3[X]);
  1313.   Max[Y] = max3(P1[Y], P2[Y], P3[Y]);
  1314.   Max[Z] = max3(P1[Z], P2[Z], P3[Z]);
  1315.  
  1316.   Make_BBox_from_min_max(*BBox, Min, Max);
  1317. }
  1318.  
  1319.  
  1320.  
  1321. /*****************************************************************************
  1322. *
  1323. * FUNCTION
  1324. *
  1325. *   Build_Mesh_BBox_Tree
  1326. *
  1327. * INPUT
  1328. *   
  1329. * OUTPUT
  1330. *   
  1331. * RETURNS
  1332. *   
  1333. * AUTHOR
  1334. *
  1335. *   Dieter Bayer
  1336. *   
  1337. * DESCRIPTION
  1338. *
  1339. *   Create the bounding box hierarchy.
  1340. *
  1341. * CHANGES
  1342. *
  1343. *   Feb 1995 : Creation. (Derived from the bounding slab creation code)
  1344. *
  1345. ******************************************************************************/
  1346.  
  1347. void Build_Mesh_BBox_Tree(Mesh)
  1348. MESH *Mesh;
  1349. {
  1350.   int i, nElem, maxelements;
  1351.   BBOX_TREE **Triangles;
  1352.  
  1353.   if (!Test_Flag(Mesh, HIERARCHY_FLAG))
  1354.   {
  1355.     return;
  1356.   }
  1357.  
  1358.   nElem = (int)Mesh->Data->Number_Of_Triangles;
  1359.  
  1360.   maxelements = 2 * nElem;
  1361.  
  1362.   /* Now allocate an array to hold references to these elements. */
  1363.  
  1364.   Triangles = (BBOX_TREE **)POV_MALLOC(maxelements*sizeof(BBOX_TREE *), "mesh bbox tree");
  1365.  
  1366.   /* Init list with mesh elements. */
  1367.  
  1368.   for (i = 0; i < nElem; i++)
  1369.   {
  1370.     Triangles[i] = (BBOX_TREE *)POV_MALLOC(sizeof(BBOX_TREE), "mesh bbox tree");
  1371.  
  1372.     Triangles[i]->Infinite = FALSE;
  1373.     Triangles[i]->Entries  = 0;
  1374.     Triangles[i]->Node     = (BBOX_TREE **)&Mesh->Data->Triangles[i];
  1375.  
  1376.     get_triangle_bbox(Mesh, &Mesh->Data->Triangles[i], &Triangles[i]->BBox);
  1377.   }
  1378.  
  1379.   Build_BBox_Tree(&Mesh->Data->Tree, nElem, Triangles, 0, NULL);
  1380.  
  1381.   /* Get rid of the Triangles array. */
  1382.  
  1383.   POV_FREE(Triangles);
  1384. }
  1385.  
  1386.  
  1387.  
  1388. /*****************************************************************************
  1389. *
  1390. * FUNCTION
  1391. *
  1392. *   intersect_bbox_tree
  1393. *
  1394. * INPUT
  1395. *
  1396. *   Mesh     - Mesh object
  1397. *   Ray      - Current ray
  1398. *   Orig_Ray - Original, untransformed ray
  1399. *   len      - Length of the transformed ray direction
  1400. *   
  1401. * OUTPUT
  1402. *
  1403. *   Depth_Stack - Stack of intersections
  1404. *   
  1405. * RETURNS
  1406. *
  1407. *   int - TRUE if an intersection was found
  1408. *   
  1409. * AUTHOR
  1410. *
  1411. *   Dieter Bayer
  1412. *   
  1413. * DESCRIPTION
  1414. *
  1415. *   Intersect a ray with the bounding box tree of a mesh.
  1416. *
  1417. * CHANGES
  1418. *
  1419. *   Feb 1995 : Creation.
  1420. *
  1421. ******************************************************************************/
  1422.  
  1423. static int intersect_bbox_tree(Mesh, Ray, Orig_Ray, len, Depth_Stack)
  1424. MESH *Mesh;
  1425. RAY *Ray, *Orig_Ray;
  1426. DBL len;
  1427. ISTACK *Depth_Stack;
  1428. {
  1429.   int i, found;
  1430.   DBL Best, Depth;
  1431.   RAYINFO rayinfo;
  1432.   BBOX_TREE *Node, *Root;
  1433.  
  1434.   /* Create the direction vectors for this ray. */
  1435.  
  1436.   Create_Rayinfo(Ray, &rayinfo);
  1437.  
  1438.   /* Start with an empty priority queue. */
  1439.  
  1440.   found = 0;
  1441.  
  1442.   Mesh_Queue->QSize = 0;
  1443.  
  1444.   Best = BOUND_HUGE;
  1445.  
  1446. #ifdef BBOX_EXTRA_STATS
  1447.   Increase_Counter(stats[totalQueueResets]);
  1448. #endif
  1449.  
  1450.   /* Check top node. */
  1451.  
  1452.   Root = Mesh->Data->Tree;
  1453.  
  1454.   /* Set the root object infinite to avoid a test. */
  1455.  
  1456.   Check_And_Enqueue(Mesh_Queue, Root, &Root->BBox, &rayinfo);
  1457.  
  1458.   /* Check elements in the priority queue. */
  1459.  
  1460.   while (Mesh_Queue->QSize > 0)
  1461.   {
  1462.     Priority_Queue_Delete(Mesh_Queue, &Depth, &Node);
  1463.  
  1464.     /*
  1465.      * If current intersection is larger than the best intersection found
  1466.      * so far our task is finished, because all other bounding boxes in
  1467.      * the priority queue are further away.
  1468.      */
  1469.  
  1470.     if (Depth > Best)
  1471.     {
  1472.       break;
  1473.     }
  1474.  
  1475.     /* Check current node. */
  1476.  
  1477.     if (Node->Entries)
  1478.     {
  1479.       /* This is a node containing leaves to be checked. */
  1480.  
  1481.       for (i = 0; i < Node->Entries; i++)
  1482.       {
  1483.         Check_And_Enqueue(Mesh_Queue, Node->Node[i], &Node->Node[i]->BBox, &rayinfo);
  1484.       }
  1485.     }
  1486.     else
  1487.     {
  1488.       /* This is a leaf so test the contained triangle. */
  1489.  
  1490.       if (intersect_mesh_triangle(Ray, Mesh, (MESH_TRIANGLE *)Node->Node, &Depth))
  1491.       {
  1492.         if (test_hit((MESH_TRIANGLE *)Node->Node, Mesh, Orig_Ray, Depth / len, Depth_Stack))
  1493.         {
  1494.           found = TRUE;
  1495.  
  1496.           Best = Depth;
  1497.         }
  1498.       }
  1499.     }
  1500.   }
  1501.  
  1502.   return(found);
  1503. }
  1504.  
  1505.  
  1506.  
  1507. /*****************************************************************************
  1508. *
  1509. * FUNCTION
  1510. *
  1511. *   mesh_hash
  1512. *
  1513. * INPUT
  1514. *
  1515. *   aPoint - Normal/Vertex to store
  1516. *   
  1517. * OUTPUT
  1518. *
  1519. *   Hash_Table - Normal/Vertex hash table
  1520. *   Number     - Number of normals/vertices
  1521. *   Max        - Max. number of normals/vertices
  1522. *   Elements   - List of normals/vertices
  1523. *   
  1524. * RETURNS
  1525. *
  1526. *   int - Index of normal/vertex into the normals/vertices list
  1527. *   
  1528. * AUTHOR
  1529. *
  1530. *   Dieter Bayer
  1531. *   
  1532. * DESCRIPTION
  1533. *
  1534. *   Try to locate a triangle normal/vertex in the normal/vertex list.
  1535. *   If the vertex is not found its stored in the normal/vertex list.
  1536. *
  1537. * CHANGES
  1538. *
  1539. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1540. *
  1541. ******************************************************************************/
  1542.  
  1543. static int mesh_hash(Hash_Table, Number, Max, Elements, aPoint)
  1544. HASH_TABLE **Hash_Table;
  1545. int *Number, *Max;
  1546. SNGL_VECT **Elements;
  1547. VECTOR aPoint;
  1548. {
  1549.   int hash;
  1550.   SNGL_VECT D, P;
  1551.   HASH_TABLE *p;
  1552.  
  1553.   Assign_SNGL_Vect(P, aPoint);
  1554.  
  1555.   /* Get hash value. */
  1556.  
  1557.   hash = (unsigned)((int)(326.0*P[X])^(int)(694.7*P[Y])^(int)(1423.6*P[Z])) % HASH_SIZE;
  1558.  
  1559.   /* Try to find normal/vertex. */
  1560.  
  1561.   for (p = Hash_Table[hash]; p != NULL; p = p->Next)
  1562.   {
  1563.     VSub(D, p->P, P);
  1564.  
  1565.     if ((fabs(D[X]) < EPSILON) && (fabs(D[Y]) < EPSILON) && (fabs(D[Z]) < EPSILON))
  1566.     {
  1567.       break;
  1568.     }
  1569.   }
  1570.  
  1571.   if ((p != NULL) && (p->Index >= 0))
  1572.   {
  1573.     return(p->Index);
  1574.   }
  1575.  
  1576.   /* Add new normal/vertex to the list and hash table. */
  1577.  
  1578.   if ((*Number) >= (*Max))
  1579.   {
  1580.     if ((*Max) >= INT_MAX/2)
  1581.     {
  1582.       Error("Too many normals/vertices in mesh.\n");
  1583.     }
  1584.  
  1585.     (*Max) *= 2;
  1586.  
  1587.     (*Elements) = (SNGL_VECT *)POV_REALLOC((*Elements), (*Max)*sizeof(SNGL_VECT), "mesh data");
  1588.   }
  1589.  
  1590.   Assign_SNGL_Vect((*Elements)[*Number], P);
  1591.  
  1592.   p = (HASH_TABLE *)POV_MALLOC(sizeof(HASH_TABLE), "mesh data");
  1593.  
  1594.   Assign_SNGL_Vect(p->P, P);
  1595.  
  1596.   p->Index = *Number;
  1597.  
  1598.   p->Next = Hash_Table[hash];
  1599.  
  1600.   Hash_Table[hash] = p;
  1601.  
  1602.   return((*Number)++);
  1603. }
  1604.  
  1605.  
  1606.  
  1607. /*****************************************************************************
  1608. *
  1609. * FUNCTION
  1610. *
  1611. *   Mesh_Hash_Vertex
  1612. *
  1613. * INPUT
  1614. *
  1615. *   Vertex - Vertex to store
  1616. *   
  1617. * OUTPUT
  1618. *
  1619. *   Number_Of_Vertices - Number of vertices
  1620. *   Max_Vertices       - Max. number of vertices
  1621. *   Vertices           - List of vertices
  1622. *   
  1623. * RETURNS
  1624. *
  1625. *   int - Index of vertex into the vertices list
  1626. *   
  1627. * AUTHOR
  1628. *
  1629. *   Dieter Bayer
  1630. *   
  1631. * DESCRIPTION
  1632. *
  1633. *   Try to locate a triangle vertex in the vertex list.
  1634. *   If the vertex is not found its stored in the vertex list.
  1635. *
  1636. * CHANGES
  1637. *
  1638. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1639. *
  1640. ******************************************************************************/
  1641.  
  1642. int Mesh_Hash_Vertex(Number_Of_Vertices, Max_Vertices, Vertices, Vertex)
  1643. int *Number_Of_Vertices, *Max_Vertices;
  1644. SNGL_VECT **Vertices;
  1645. VECTOR Vertex;
  1646. {
  1647.   return(mesh_hash(Vertex_Hash_Table, Number_Of_Vertices, Max_Vertices, Vertices, Vertex));
  1648. }
  1649.  
  1650.  
  1651.  
  1652. /*****************************************************************************
  1653. *
  1654. * FUNCTION
  1655. *
  1656. *   Mesh_Hash_Normal
  1657. *
  1658. * INPUT
  1659. *
  1660. *   Normal - Normal to store
  1661. *   
  1662. * OUTPUT
  1663. *
  1664. *   Number_Of_Normals - Number of normals
  1665. *   Max_Normals       - Max. number of normals
  1666. *   Normals           - List of normals
  1667. *   
  1668. * RETURNS
  1669. *
  1670. *   int - Index of normal into the normals list
  1671. *   
  1672. * AUTHOR
  1673. *
  1674. *   Dieter Bayer
  1675. *   
  1676. * DESCRIPTION
  1677. *
  1678. *   Try to locate a triangle normal in the normal list.
  1679. *   If the normal is not found its stored in the normal list.
  1680. *
  1681. * CHANGES
  1682. *
  1683. *   Feb 1995 : Creation. (With help from Steve Anger's RAW2POV code)
  1684. *
  1685. ******************************************************************************/
  1686.  
  1687. int Mesh_Hash_Normal(Number_Of_Normals, Max_Normals, Normals, S_Normal)
  1688. int *Number_Of_Normals, *Max_Normals;
  1689. SNGL_VECT **Normals;
  1690. VECTOR S_Normal;
  1691. {
  1692.   return(mesh_hash(Normal_Hash_Table, Number_Of_Normals, Max_Normals, Normals, S_Normal));
  1693. }
  1694.  
  1695.  
  1696.  
  1697. /*****************************************************************************
  1698. *
  1699. * FUNCTION
  1700. *
  1701. *   Mesh_Hash_Texture
  1702. *
  1703. * INPUT
  1704. *
  1705. *   Texture - Texture to store
  1706. *   
  1707. * OUTPUT
  1708. *
  1709. *   Number_Of_Textures - Number of textures
  1710. *   Max_Textures       - Max. number of textures
  1711. *   Textures           - List of textures
  1712. *   
  1713. * RETURNS
  1714. *
  1715. *   int - Index of texture into the texture list
  1716. *   
  1717. * AUTHOR
  1718. *
  1719. *   Dieter Bayer
  1720. *   
  1721. * DESCRIPTION
  1722. *
  1723. *   Try to locate a texture in the texture list.
  1724. *   If the texture is not found its stored in the texture list.
  1725. *
  1726. * CHANGES
  1727. *
  1728. *   Feb 1995 : Creation.
  1729. *
  1730. ******************************************************************************/
  1731.  
  1732. int Mesh_Hash_Texture(Number_Of_Textures, Max_Textures, Textures, Texture)
  1733. int *Number_Of_Textures, *Max_Textures;
  1734. TEXTURE ***Textures, *Texture;
  1735. {
  1736.   int i;
  1737.  
  1738.   if (Texture == NULL)
  1739.   {
  1740.     return(-1);
  1741.   }
  1742.  
  1743.   /* Just do a linear search. */
  1744.  
  1745.   for (i = 0; i < *Number_Of_Textures; i++)
  1746.   {
  1747.     if ((*Textures)[i] == Texture)
  1748.     {
  1749.       break;
  1750.     }
  1751.   }
  1752.  
  1753.   if (i == *Number_Of_Textures)
  1754.   {
  1755.     if ((*Number_Of_Textures) >= (*Max_Textures))
  1756.     {
  1757.       if ((*Max_Textures) >= INT_MAX/2)
  1758.       {
  1759.         Error("Too many textures in mesh.\n");
  1760.       }
  1761.  
  1762.       (*Max_Textures) *= 2;
  1763.  
  1764.       (*Textures) = (TEXTURE **)POV_REALLOC((*Textures), (*Max_Textures)*sizeof(TEXTURE *), "mesh data");
  1765.     }
  1766.  
  1767.     (*Textures)[(*Number_Of_Textures)++] = Copy_Texture_Pointer(Texture);
  1768.   }
  1769.  
  1770.   return(i);
  1771. }
  1772.  
  1773.  
  1774.  
  1775. /*****************************************************************************
  1776. *
  1777. * FUNCTION
  1778. *
  1779. *   Create_Mesh_Hash_Tables
  1780. *
  1781. * INPUT
  1782. *   
  1783. * OUTPUT
  1784. *   
  1785. * RETURNS
  1786. *   
  1787. * AUTHOR
  1788. *
  1789. *   Dieter Bayer
  1790. *   
  1791. * DESCRIPTION
  1792. *
  1793. *   -
  1794. *
  1795. * CHANGES
  1796. *
  1797. *   Feb 1995 : Creation.
  1798. *
  1799. ******************************************************************************/
  1800.  
  1801. void Create_Mesh_Hash_Tables()
  1802. {
  1803.   int i;
  1804.  
  1805.   Vertex_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1806.  
  1807.   for (i = 0; i < HASH_SIZE; i++)
  1808.   {
  1809.     Vertex_Hash_Table[i] = NULL;
  1810.   }
  1811.  
  1812.   Normal_Hash_Table = (HASH_TABLE **)POV_MALLOC(HASH_SIZE*sizeof(HASH_TABLE *), "mesh hash table");
  1813.  
  1814.   for (i = 0; i < HASH_SIZE; i++)
  1815.   {
  1816.     Normal_Hash_Table[i] = NULL;
  1817.   }
  1818. }
  1819.  
  1820.  
  1821.  
  1822. /*****************************************************************************
  1823. *
  1824. * FUNCTION
  1825. *
  1826. *   Destroy_Mesh_Hash_Tables
  1827. *
  1828. * INPUT
  1829. *   
  1830. * OUTPUT
  1831. *   
  1832. * RETURNS
  1833. *   
  1834. * AUTHOR
  1835. *
  1836. *   Dieter Bayer
  1837. *   
  1838. * DESCRIPTION
  1839. *
  1840. *   -
  1841. *
  1842. * CHANGES
  1843. *
  1844. *   Feb 1995 : Creation.
  1845. *
  1846. ******************************************************************************/
  1847.  
  1848. void Destroy_Mesh_Hash_Tables()
  1849. {
  1850.   int i;
  1851.   HASH_TABLE *Temp;
  1852.  
  1853.   for (i = 0; i < HASH_SIZE; i++)
  1854.   {
  1855.     while (Vertex_Hash_Table[i] != NULL)
  1856.     {
  1857.       Temp = Vertex_Hash_Table[i];
  1858.       
  1859.       Vertex_Hash_Table[i] = Temp->Next;
  1860.       
  1861.       POV_FREE(Temp);
  1862.     }
  1863.   }
  1864.  
  1865.   POV_FREE(Vertex_Hash_Table);
  1866.  
  1867.   for (i = 0; i < HASH_SIZE; i++)
  1868.   {
  1869.     while (Normal_Hash_Table[i] != NULL)
  1870.     {
  1871.       Temp = Normal_Hash_Table[i];
  1872.       
  1873.       Normal_Hash_Table[i] = Temp->Next;
  1874.       
  1875.       POV_FREE(Temp);
  1876.     }
  1877.   }
  1878.  
  1879.   POV_FREE(Normal_Hash_Table);
  1880. }
  1881.  
  1882.  
  1883.  
  1884. /*****************************************************************************
  1885. *
  1886. * FUNCTION
  1887. *
  1888. *   get_triangle_vertices
  1889. *
  1890. * INPUT
  1891. *
  1892. *   Mesh     - Mesh object
  1893. *   Triangle - Triangle
  1894. *   
  1895. * OUTPUT
  1896. *   
  1897. * RETURNS
  1898. *
  1899. *   P1, P2, P3 - Vertices of the triangle
  1900. *   
  1901. * AUTHOR
  1902. *
  1903. *   Dieter Bayer
  1904. *   
  1905. * DESCRIPTION
  1906. *
  1907. *   -
  1908. *
  1909. * CHANGES
  1910. *
  1911. *   Feb 1995 : Creation.
  1912. *
  1913. ******************************************************************************/
  1914.  
  1915. static void get_triangle_vertices(Mesh, Triangle, P1, P2, P3)
  1916. MESH *Mesh;
  1917. MESH_TRIANGLE *Triangle;
  1918. VECTOR P1, P2, P3;
  1919. {
  1920.   Assign_SNGL_Vect(P1, Mesh->Data->Vertices[Triangle->P1]);
  1921.   Assign_SNGL_Vect(P2, Mesh->Data->Vertices[Triangle->P2]);
  1922.   Assign_SNGL_Vect(P3, Mesh->Data->Vertices[Triangle->P3]);
  1923. }
  1924.  
  1925.  
  1926.  
  1927. /*****************************************************************************
  1928. *
  1929. * FUNCTION
  1930. *
  1931. *   get_triangle_normals
  1932. *
  1933. * INPUT
  1934. *
  1935. *   Mesh     - Mesh object
  1936. *   Triangle - Triangle
  1937. *   
  1938. * OUTPUT
  1939. *   
  1940. * RETURNS
  1941. *
  1942. *   N1, N2, N3 - Normals of the triangle
  1943. *   
  1944. * AUTHOR
  1945. *
  1946. *   Dieter Bayer
  1947. *   
  1948. * DESCRIPTION
  1949. *
  1950. *   -
  1951. *
  1952. * CHANGES
  1953. *
  1954. *   Feb 1995 : Creation.
  1955. *
  1956. ******************************************************************************/
  1957.  
  1958. static void get_triangle_normals(Mesh, Triangle, N1, N2, N3)
  1959. MESH *Mesh;
  1960. MESH_TRIANGLE *Triangle;
  1961. VECTOR N1, N2, N3;
  1962. {
  1963.   Assign_SNGL_Vect(N1, Mesh->Data->Normals[Triangle->N1]);
  1964.   Assign_SNGL_Vect(N2, Mesh->Data->Normals[Triangle->N2]);
  1965.   Assign_SNGL_Vect(N3, Mesh->Data->Normals[Triangle->N3]);
  1966. }
  1967.  
  1968.  
  1969.  
  1970. /*****************************************************************************
  1971. *
  1972. * FUNCTION
  1973. *
  1974. *   Mesh_Degenerate
  1975. *
  1976. * INPUT
  1977. *
  1978. *   P1, P2, P3 - Triangle's vertices
  1979. *   
  1980. * OUTPUT
  1981. *   
  1982. * RETURNS
  1983. *
  1984. *   int - TRUE if degenerate
  1985. *   
  1986. * AUTHOR
  1987. *
  1988. *   Dieter Bayer
  1989. *   
  1990. * DESCRIPTION
  1991. *
  1992. *   Test if a triangle is degenerate.
  1993. *
  1994. * CHANGES
  1995. *
  1996. *   Feb 1995 : Creation.
  1997. *
  1998. ******************************************************************************/
  1999.  
  2000. int Mesh_Degenerate(P1, P2, P3)
  2001. VECTOR P1, P2, P3;
  2002. {
  2003.   VECTOR V1, V2, Temp;
  2004.   DBL Length;
  2005.  
  2006.   VSub(V1, P1, P2);
  2007.   VSub(V2, P3, P2);
  2008.  
  2009.   VCross(Temp, V1, V2);
  2010.  
  2011.   VLength(Length, Temp);
  2012.  
  2013.   return(Length == 0.0);
  2014. }
  2015.  
  2016.  
  2017. /*****************************************************************************
  2018. *
  2019. * FUNCTION
  2020. *
  2021. *   Initialize_Mesh_Code
  2022. *
  2023. * INPUT
  2024. *   
  2025. * OUTPUT
  2026. *   
  2027. * RETURNS
  2028. *   
  2029. * AUTHOR
  2030. *
  2031. *   Dieter Bayer
  2032. *   
  2033. * DESCRIPTION
  2034. *
  2035. *   Initialize mesh specific variables.
  2036. *
  2037. * CHANGES
  2038. *
  2039. *   Jul 1995 : Creation.
  2040. *
  2041. ******************************************************************************/
  2042.  
  2043. void Initialize_Mesh_Code()
  2044. {
  2045.   Mesh_Queue = Create_Priority_Queue(INITIAL_NUMBER_OF_ENTRIES);
  2046. }
  2047.  
  2048.  
  2049.  
  2050. /*****************************************************************************
  2051. *
  2052. * FUNCTION
  2053. *
  2054. *   Deinitialize_Mesh_Code
  2055. *
  2056. * INPUT
  2057. *   
  2058. * OUTPUT
  2059. *   
  2060. * RETURNS
  2061. *   
  2062. * AUTHOR
  2063. *
  2064. *   Dieter Bayer
  2065. *   
  2066. * DESCRIPTION
  2067. *
  2068. *   Deinitialize mesh specific variables.
  2069. *
  2070. * CHANGES
  2071. *
  2072. *   Jul 1995 : Creation.
  2073. *
  2074. ******************************************************************************/
  2075.  
  2076. void Deinitialize_Mesh_Code()
  2077. {
  2078.   if (Mesh_Queue != NULL)
  2079.   {
  2080.     Destroy_Priority_Queue(Mesh_Queue);
  2081.   }
  2082.  
  2083.   Mesh_Queue = NULL;
  2084. }
  2085.  
  2086.  
  2087.  
  2088. /*****************************************************************************
  2089. *
  2090. * FUNCTION
  2091. *
  2092. *   Test_Mesh_Opacity
  2093. *
  2094. * INPUT
  2095. *
  2096. *   Mesh - Pointer to mesh structure
  2097. *
  2098. * OUTPUT
  2099. *
  2100. *   Mesh
  2101. *
  2102. * RETURNS
  2103. *
  2104. * AUTHOR
  2105. *
  2106. *   Dieter Bayer
  2107. *
  2108. * DESCRIPTION
  2109. *
  2110. *   Set the opacity flag of the mesh according to the opacity
  2111. *   of the mesh's texture(s).
  2112. *
  2113. * CHANGES
  2114. *
  2115. *   Apr 1996 : Creation.
  2116. *
  2117. ******************************************************************************/
  2118.  
  2119. void Test_Mesh_Opacity(Mesh)
  2120. MESH *Mesh;
  2121. {
  2122.   int i;
  2123.  
  2124.   /* Initialize opacity flag to the opacity of the object's texture. */
  2125.  
  2126.   if ((Mesh->Texture == NULL) || (Test_Opacity(Mesh->Texture)))
  2127.   {
  2128.     Set_Flag(Mesh, OPAQUE_FLAG);
  2129.   }
  2130.  
  2131.   if (Test_Flag(Mesh, MULTITEXTURE_FLAG))
  2132.   {
  2133.     for (i = 0; i < Mesh->Data->Number_Of_Textures; i++)
  2134.     {
  2135.       if (Mesh->Data->Textures[i] != NULL)
  2136.       {
  2137.         /* If component's texture isn't opaque the mesh is neither. */
  2138.  
  2139.         if (!Test_Opacity(Mesh->Data->Textures[i]))
  2140.         {
  2141.           Clear_Flag(Mesh, OPAQUE_FLAG);
  2142.         }
  2143.       }
  2144.     }
  2145.   }
  2146. }
  2147.  
  2148.  
  2149.  
  2150.